package org.zn.etl.job.transformer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.tuple.Pair;
import org.zn.datax.common.entity.ColumnInfo;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;

/**
 * @author zhounan
 * @date  2025年10月15日
 * @description
 */
public class GroovyScriptRowTrans extends RowDataTransformer {

	protected GroovyShell shell=new GroovyShell();
	
	protected Script script;
	
	
	public void initScript(String scriptStr) {
		
		script= shell.parse(scriptStr);
		
	}

	@Override
	public void transRow(Map<String, Object> row, List<Pair<ColumnInfo, ColumnInfo>> columnMappingPairs) {
		// TODO Auto-generated method stub

		Map<String, Object> bindMap=new HashMap<>();
		bindMap.put("row", row);
		bindMap.put("columnMappingPairs", columnMappingPairs);

		Binding bind=new Binding(bindMap);

		script.setBinding(bind);
		script.run();
		
	}
	
	
	public static void main(String[] args) {
		
		GroovyScriptRowTrans t=new GroovyScriptRowTrans();
		
		  t.initScript(" number= number*2 ;"
		  		+ " println 'test groovy '+number;");
		
		  Map<String, Object> map=new HashMap<>();
		  
		  Binding bind=new Binding(map);
		  for (int i = 0; i < 1000; i++) {
			  		
		Integer number=new Integer(i);	  
			  
		map.put("number", number);
			
		t.script.setBinding(bind);	
			
		t.script.run();
		
		System.out.println("get return:"+bind.getVariable("number") +"  map obj:"+map.get("number")); 
			
			
		}
		
		
		
	}
	

}
